async function checkPreviousAchievement() {
    try {
        // Get last played info
        const lastPlayed = JSON.parse(localStorage.getItem('lastPlayed') || '{}');
        const today = new Date().toISOString().split('T')[0];
        
        // If no previous play or already shown today, return null
        if (!lastPlayed.name || !lastPlayed.date || lastPlayed.lastShownDate === today) {
            return null;
        }

        // Get the leaderboard for that specific date
        const trickleObjAPI = new TrickleObjectAPI();
        const { items: scores } = await trickleObjAPI.listObjects(
            `colorGameScore_${lastPlayed.date.replace(/-/g, '')}`,
            1000000,
            true
        );

        if (!scores || scores.length === 0) {
            return null;
        }

        // Sort scores and find player's position
        const sortedScores = scores.sort((a, b) => {
            if (b.objectData.score !== a.objectData.score) {
                return b.objectData.score - a.objectData.score;
            }
            return new Date(a.objectData.timestamp) - new Date(b.objectData.timestamp);
        });

        const playerScore = sortedScores.find(
            score => score.objectData.playerName.toLowerCase() === lastPlayed.name.toLowerCase()
        );

        if (!playerScore) {
            return null;
        }

        const position = sortedScores.findIndex(
            score => score.objectData.playerName.toLowerCase() === lastPlayed.name.toLowerCase()
        ) + 1;

        const percentile = Math.round(((scores.length - position) / scores.length) * 100);

        // Update last shown date
        localStorage.setItem('lastPlayed', JSON.stringify({
            ...lastPlayed,
            lastShownDate: today
        }));

        return {
            name: lastPlayed.name,
            date: lastPlayed.date,
            score: playerScore.objectData.score,
            position,
            totalPlayers: scores.length,
            percentile
        };
    } catch (error) {
        reportError(error);
        return null;
    }
}

function updateLastPlayed(playerName) {
    try {
        const today = new Date().toISOString().split('T')[0];
        localStorage.setItem('lastPlayed', JSON.stringify({
            name: playerName,
            date: today,
            lastShownDate: today
        }));
    } catch (error) {
        reportError(error);
    }
}
